/**
 * 同步
 */

class Demo extends  Thread {
    private int ticket = 5;
    @Override
    public void run() {
        for (int i = 0; i <= ticket; i++) {
            buy();
        }
    }
    public synchronized void buy() {
        if (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("车票" + ticket--);
        }
    }
}

public class SynchronzedTest {
    public static void main(String[] args) {
        Demo demo = new Demo();
        Thread t1 = new Thread(demo);
        Thread t2 = new Thread(demo);
        t1.start();
        t2.start();
    }
}

